package cn.tedu.straw.api.question.controller;

import cn.tedu.straw.api.question.ex.InvalidParameterException;
import cn.tedu.straw.api.question.ex.QuestionNotFoundException;
import cn.tedu.straw.api.question.ex.ServiceException;
import cn.tedu.straw.commons.util.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({ServiceException.class})
    public R handleException(Exception e) {
        if (e instanceof InvalidParameterException) {
            return R.failure(R.State.ERR_INVALID_PARAMETER, e);
        } else if (e instanceof QuestionNotFoundException) {
            return R.failure(R.State.ERR_QUESTION_NOT_FOUND, e);
        }

        e.printStackTrace();
        return R.failure(-999999, "出现了未知错误，请检查控制台！");
    }

}
